// pages/bleDemo/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: 0,
    msg: "蓝牙正在适配...",
    searchDevice: false,
    deviceList: [],
    currentDevice: {},
    nowDevice: false,
    deviceService: {},
    nowService: false,
    serviceId: "",
    inputValue: "",
    sendData: "",
    dataResult: {},
    writeDatas: "",
    reMessage: [],
    deviceId: "",
  },
  // 获取当前设备信息
  getSystemInfo() {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res, "获取当前设备信息");
        that.setData({
          windowHeight: res.windowHeight,
        });
      },
    });
  },
  // 初始化蓝牙模块
  openBluetooth() {
    var that = this;
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log("初始化蓝牙适配器成功" + JSON.stringify(res));
        that.setData({
          msg: "初始化蓝牙适配器成功",
        });
        wx.showModal({
          title: "蓝牙适配情况",
          content: "初始化蓝牙适配器成功",
        });
      },
      fail() {
        that.setData({
          msg: "初始化蓝牙适配器失败",
        });
        wx.showModal({
          title: "蓝牙适配情况",
          content: "蓝牙适配失败，请检查手机蓝牙和定位功能是否打开",
        });
      },
      complete() {
        console.log("初始化蓝牙适配器完成");
      },
    });
  },
  // 开启蓝牙设备搜索
  switchChange() {
    var that = this;
    if (!that.data.searchDevice) {
      that.setData({
        searchDevice: !that.data.searchDevice,
      });
      console.log("打开蓝牙设备搜索");
      wx.getBluetoothAdapterState({
        success(res) {
          console.log("getBluetoothAdapterState", res);
        },
      });
      wx.onBluetoothAdapterStateChange(function (res) {
        console.log("onBluetoothAdapterStateChange", res.available);
      });
      wx.startBluetoothDevicesDiscovery({
        success(res) {
          console.log("startBluetoothDevicesDiscovery", res);
        },
      });
      wx.onBluetoothDeviceFound(function (devices) {
        console.error(devices, "devices");
        if (!devices.devices[0]) return;
        let result = that.data.deviceList.some((item) => {
          console.log(item, "item");
          if (item.deviceId === devices.devices[0].deviceId) return true;
        });

        if (!result) {
          that.setData({
            deviceList: [...that.data.deviceList, devices.devices[0]],
          });
        }

        // console.log('onBluetoothDeviceFound', that.data.deviceList)
      });
      wx.getBluetoothDevices({
        success(res) {
          console.log("getBluetoothDevices", res);
        },
      });
    } else {
      // that.deviceList = []
      // that.currentDevice = {}
      wx.stopBluetoothDevicesDiscovery({
        success(res) {
          that.setData({
            searchDevice: !that.data.searchDevice,
          });
          console.log("关闭蓝牙设备搜索", res);
        },
      });
    }
  },
  // 点击蓝牙设备
  chooseDevice(event) {
    console.log(event.currentTarget.dataset.item, "item");
    var that = this;
    that.setData({
      currentDevice: event.currentTarget.dataset.item,
      nowDevice: true,
    });
    wx.pageScrollTo({
      scrollTop: that.data.windowHeight,
      duration: 100,
    });
  },
  //
  confirmConnect(event) {
    console.log(event.currentTarget, "aaaaaa");
    var that = this;
    wx.showModal({
      title: "连接当前蓝牙设备",
      success(res) {
        if (res.confirm) {
          that.connectTo(event.currentTarget.dataset.deviceid);
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  //
  connectTo(deviceId) {
    var that = this;
    console.log("连接蓝牙设备搜索", deviceId);
    wx.stopBluetoothDevicesDiscovery({
      success(res) {
        console.log("关闭蓝牙设备搜索", res);
        that.setData({
          searchDevice: !that.data.searchDevice,
        });
      },
    });
    wx.showLoading({
      title: "连接蓝牙设备中...",
    });
    wx.createBLEConnection({
      deviceId: deviceId,
      success(res) {
        console.log("蓝牙设备连接成功", res);
        wx.hideLoading();
        wx.getBLEDeviceServices({
          deviceId: deviceId,
          success(res) {
            that.setData({
              deviceService: res.services,
            });
            for (var t = 0; t < that.data.deviceService.length; t++) {
              var service = that.data.deviceService[t];
              var serviceId = service.uuid.substring(4, 8);
              console.log(service.uuid, "serviceId1111111");
              if (serviceId === "FFE0") {
                that.setData({
                  serviceId: service.uuid,
                });
              }
            }
            that.setData({
              nowDevice: !that.nowDevice,
              nowService: !that.nowService,
            });
            console.log("获取蓝牙设备Service" + res.errMsg);
          },
          fail(res) {
            wx.showModal({
              title: "设备Service信息",
              content:
                "蓝牙设备连接成功" + "\n" + "设备信息获取错误" + res.errMsg,
            });
          },
        });
      },
      fail(res) {
        console.log("蓝牙设备连接失败，请稍后重试");
        wx.hideLoading();
        wx.showModal({
          title: "提示",
          content: "蓝牙设备连接失败，请稍后重试",
          duration: 2000,
        });
      },
      complete() {
        console.log("蓝牙设备连接完成");
        wx.hideLoading();
      },
    });
  },
  // 发送数据
  sendMessage() {
    let that = this;
    console.log(that.data.sendData);
    let data = that.data.sendData.split(",");
    let dataBuffer = new ArrayBuffer(data.length);
    let dataView = new DataView(dataBuffer);
    for (let j = 0; j < data.length; j++) {
      dataView.setUint8(j, "0x" + data[j]);
    }
    that.setData({
      deviceId: that.data.currentDevice.deviceId,
    });
    console.log(that.data.serviceId, "serviceId");
    console.log(that.data.deviceId, "deviceId");
    wx.getBLEDeviceCharacteristics({
      deviceId: that.data.deviceId,
      serviceId: that.data.serviceId,
      success(res) {
        console.log(res.characteristics);
        that.deviceCharacteristics = res.characteristics;
        for (var i = 0; i < that.deviceCharacteristics.length; i++) {
          that.characteristic = that.deviceCharacteristics[i];
          that.characteristicProperties = that.characteristic.properties;
          if (that.characteristicProperties.notify === true) {
            that.characteristicId = that.characteristic.uuid;
            wx.notifyBLECharacteristicValueChange({
              state: true, // 启用 notify 功能
              deviceId: that.deviceId,
              serviceId: that.serviceId,
              characteristicId: that.characteristicId,
              success(res) {
                console.log("开启notify成功" + that.characteristic.uuid);
                for (let i = 0; i < dataView.byteLength; i++) {
                  var writeData = "0x" + dataView.getUint8(i).toString(16);
                  that.writeDatas = that.writeDatas + "\n" + writeData;
                }
                wx.writeBLECharacteristicValue({
                  deviceId: that.deviceId,
                  serviceId: that.serviceId,
                  characteristicId: that.characteristicId,
                  value: dataBuffer,
                  success(res) {
                    console.log("发送的数据：" + that.writeDatas);
                    console.log("message发送成功");
                    wx.showModal({
                      title: "数据发送成功",
                      content: that.writeDatas,
                    });
                    wx.readBLECharacteristicValue({
                      deviceId: that.deviceId,
                      serviceId: that.serviceId,
                      characteristicId: that.characteristicId,
                      success(res) {
                        console.log("读取数据成功");
                      },
                    });
                  },
                  fail(res) {
                    // fail
                    console.log("message发送失败" + that.characteristicIdw);
                    wx.showToast({
                      title: "数据发送失败，请稍后重试",
                      icon: "none",
                    });
                  },
                  complete(res) {
                    // fail
                    console.log("message发送完成");
                  },
                });
              },
              fail() {
                console.log("开启notify失败" + that.characteristicId);
              },
            });
            // that.writeMessage(that.deviceId, that.serviceId, that.characteristicIdw, that.characteristicIdr, that.characteristicIdn)
          }
        }
      },
      fail() {
        console.log("获取characteristic失败");
      },
    });
    that.writeDatas = [];
  },

  receiveMessage() {
    let that = this;
    that.deviceId = this.currentDevice.deviceId;
    that.serviceId = that.serviceId;
    wx.getBLEDeviceCharacteristics({
      deviceId: that.deviceId,
      serviceId: that.serviceId,
      success(res) {
        console.log(res.characteristics);
        that.deviceCharacteristics = res.characteristics;
        for (var i = 0; i < that.deviceCharacteristics.length; i++) {
          that.characteristic = that.deviceCharacteristics[i];
          that.characteristicProperties = that.characteristic.properties;
          if (that.characteristicProperties.notify === true) {
            that.characteristicId = that.characteristic.uuid;
            wx.notifyBLECharacteristicValueChange({
              state: true, // 启用 notify 功能
              deviceId: that.deviceId,
              serviceId: that.serviceId,
              characteristicId: that.characteristicId,
              success(res) {
                console.log("开启notify成功" + that.characteristic.uuid);
                console.log(that.characteristicProperties.write);
                console.log(that.characteristicProperties.read);
                wx.readBLECharacteristicValue({
                  deviceId: that.deviceId,
                  serviceId: that.serviceId,
                  characteristicId: that.characteristicId,
                  success(res) {
                    console.log("接收数据成功");
                  },
                });
                wx.onBLECharacteristicValueChange(function (res) {
                  console.log("接收数据:", that.ab2hex(res.value));
                  var recMessage = that.ab2hex(res.value);
                  that.reMessage.push(recMessage);
                });
              },
              fail() {
                console.log("开启notify失败" + that.characteristicId);
                console.log(that.characteristicProperties.write);
                console.log(that.characteristicProperties.read);
              },
              complete() {
                console.log("开启完成");
              },
            });
          }
        }
      },
      fail() {
        console.log("获取characteristic失败");
      },
    });
  },
  disconnect() {
    var that = this;
    that.setData({
      deviceId: that.data.currentDevice.deviceId,
    });
    wx.closeBLEConnection({
      deviceId: that.data.deviceId,
      success(res) {
        wx.showModal({
          title: "设备连接情况",
          content: "蓝牙设备已经断开",
        });
        that.setData({
          nowDevice: false,
          currentDevice: {},
          nowService: false,
          deviceService: {},
        });
      },
    });
  },
  ab2hex(buffer) {
    var hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
        return ("00" + bit.toString(16)).slice(-2);
      }
    );
    return hexArr.join("");
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.deviceList, "deviceList");
    this.getSystemInfo();
    this.openBluetooth();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 监听蓝牙适配器状态变化事件
    wx.onBluetoothAdapterStateChange(function (res) {
      console.log(`蓝牙设备状态更改`, res.available);
      that.openBluetooth();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
